package com.df.bpm.utils.businessViewerV2.informatization;

import com.df.bpm.utils.businessViewerV2.Main;
import com.df.bpm.utils.businessViewerV2.res.V_scrollPanel;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class V_scroll4Target extends V_scrollPanel<M_target> {

    M_requirement requirementBasic;

    @Override
    public List<String> getColNames() {
        return Arrays.asList("ID", "序号","目标","创建时间","更新时间");
    }

    @Override
    public void hideCol(JTable iTable) {
        Main.hideCol(iTable,0);
    }

    @Override
    public List<Integer> colWidthSetting() {
        return new ArrayList<>(Arrays.asList(1,3,11,11,11));
    }

    @Override
    public Object[] getRowValues(M_target row) {
        return new Object[]{row.getInformatizationTargetIndex(),-1,row.getTarget(),
                dateFormat.format(row.getCreateDate()),
                dateFormat.format(row.getUpdateDate())};
    }

    @Override
    public M_target getEmptyRow() {
        return new M_target();
    }

    @Override
    public MouseListener mouseAdapter4Table(JTable iTable, List<M_target> list) {
        return new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int row = iTable.rowAtPoint(e.getPoint());
                int col = iTable.columnAtPoint(e.getPoint());
                if (row >= 0) {
                    if (e.getClickCount() == 2) {// 处理双击事件
                        if ((col == 2)) {//编辑需求改善目标
                            Object oldValue = iTable.getValueAt(row, 2);
                            String tips = "请输入改善目标：";
                            String newValue = (String) JOptionPane.showInputDialog(null, tips, "类型名称", JOptionPane.PLAIN_MESSAGE, null, null, oldValue);
                            if (newValue != null && !newValue.isEmpty() && !newValue.equals(oldValue)) {
                                list.get(row).setTarget(newValue);
                                iTable.setValueAt(newValue, row, col);
                                if (row == list.size() - 1) {
                                    list.add(getEmptyRow());
                                    ((DefaultTableModel)iTable.getModel()).addRow(new Object[getColNames().size()]);
                                }
                                if(list.get(row).getInformatizationTargetIndex()!=null){
                                    if(newValue.equals("-")){//删
                                        C_requirement.deleteTarget(list.get(row));
                                        iTable.setValueAt(null, row, 0);
                                        list.get(row).setInformatizationTargetIndex(null);
                                    }else{//改
                                        if(requirementBasic!=null &&requirementBasic.getInformatizationRequirementIndex()!=null){
                                            list.get(row).setInformatizationRequirementIndex(requirementBasic.getInformatizationRequirementIndex());
                                        }
                                        C_requirement.updateTarget(list.get(row));
                                    }
                                }else{//增
                                    if(requirementBasic!=null && requirementBasic.getInformatizationRequirementIndex()!=null){
                                        list.get(row).setInformatizationRequirementIndex(requirementBasic.getInformatizationRequirementIndex());
                                        M_target mTarget = C_requirement.addTarget(requirementBasic, list.get(row));
                                        list.set(row,mTarget);
                                        iTable.setValueAt(dateFormat.format(mTarget.getCreateDate()), row, 3);
                                    }
                                }
                            }
                        }

                    }
                }
            }
        };
    }

    public void setRequirementBasic(M_requirement basic) {
        this.requirementBasic=basic;
    }
}
